thin.imports('/public/configure/conf.activity.js');
thin.imports('/public/configure/conf.project.js');
thin.imports('/public/plug/LoadEffect/LoadEffect.js');

thin.use(function() {
	// 頁面加載完畢
	thin.effect();

	actid = thin.data['data-id'];
	if(actid > 0) {
		layer.load(0, {
			shade: [0.08, '#000']
		});

		// 獲取活動詳情
		__getdata_actdetail['id'] = actid;
		factory.request('_activity_get');
	}

	$(document).on('click', '.prolists', function(e) {
		proid = $(this).attr('data-id');
		$(this).addClass('active').siblings().removeClass('active');
	});
});

var actid,
	proid,
	__getdata_actdetail = {},
	__getdata_actup = {},
	__getdata_actout = {};

// 獲取活動詳情回調
function __complete_actdetail(res) {
	layer.closeAll('loading');
	if(res.status == 200) {
		binddetail(res.data);
	} else {
		layer.msg(res.error, {
			icon: 2,
			time: 800
		});
	}
}

// 綁定信息
function binddetail(data) {
	$('#bind-de-cover').attr('src', data.cover);
	$('#bind-de-title').text(data.title);
	$('#bind-de-start_time').text(tool.formatDate('yy-mm-dd hh:ii', data.start_time));
	$('#bind-de-end_time').text(tool.formatDate('yy-mm-dd hh:ii', data.end_time));
	$('#bind-de-position').text(data.localtion + '  ' + data.address);
	$('#bind-de-organizers').text(data.organizers);
	var val = data.introduction;
	val = val.replace(/&lt;/gi, '<');
	testEditormdView = editormd.markdownToHTML("bind-de-introduction", {
		markdown: val,
		htmlDecode: "style,script,iframe", // you can filter tags decode
		tocm: true, // Using [TOCM]
		emoji: true,
		taskList: true,
		tex: true, // 默认不解析
		flowChart: true, // 默认不解析
		sequenceDiagram: true, // 默认不解析
	});
	// 判斷是否外鏈活動，0=自主活動，1=外鏈活動
	if(data.type == 0) {
		// 判斷是否有人報名
		if(data.sign_up_count > 0) {
			$('#bind-de-sign_up_count').text(data.sign_up_count);
			var ele = '';
			for(var i in data.sign_top10_list) {
				var list = data.sign_top10_list[i];
				ele += '<img src="' + list.headimgurl + '" class="pic-circle" />';
			}
			$('#bind-de-sith').html(ele);
			$('#haspeo').show();
		}

		if(new Date().getTime() > (data.end_time * 1000)) {
			// 活動已過期
		} else if(data.is_sign_up == 1) {
			$('#btn-sing').text('取消报名').removeClass('reset-buttom').addClass('submit-buttom bg-e64340').off('click').on('click', deSingOut).attr('href', 'javascript:;');
		} else {
			$('#btn-sing').text('立即报名').removeClass('reset-buttom').addClass('submit-buttom').off('click').on('click', deSingUp).attr('href', 'javascript:;');
		}
	} else {
		$('#haspeo').hide();
		if(new Date().getTime() > (data.end_time * 1000)) {
			// 活動已過期
		} else {
			$('#btn-sing').text('立即报名').removeClass('reset-buttom').addClass('submit-buttom').off('click').attr('href', data.sign_up_url);
		}
	}
}

// 活動報名
function deSingUp() {
	layer.load(0, {
		shade: [0.08, '#000']
	});
	__getdata_actup['activity_id'] = actid;
	// 獲取項目列表
	factory.request('_project_cxlist');
}

// 獲取項目列表回調
function __complete_procxlist(res) {
	layer.closeAll('loading');
	if(res.status == 200) {
		if(res.data.length <= 0) {
			layer.confirm('您还没有创建项目，暂时无法报名参与', {
					btn: ['前往创建项目', '再看看']
				},
				function() {
					window.location.href = '/project/create.html';
				},
				function(index) {
					layer.close(index);
				});
			return;
		}

		var ele = '';
		ele += '<div class="p-x-3x p-y-3x clearfix">';
		for(var i in res.data) {
			var data = res.data[i];
			ele += '<a href="javascript:;" class="prolists" data-id="' + data.id + '">';
			ele += '<img src="' + data.logo + '" class="pic-circle m-x-auto" />';
			ele += '<p class="ellipsis text-center fs14r">' + data.title + '</p>';
			ele += '</a>';
		}
		ele += '</div>';

		layer.open({
			type: 1,
			title: '选择项目',
			content: ele,
			area: ['532px', 'auto'],
			btn: ['确定', '取消'],
			yes: function(index) {
				if(!proid) {
					layer.msg('至少选择一个项目', {
						time: 1200
					});
					return;
				}
				__getdata_actup['project_id'] = proid;
				factory.request('_activity_up');
				layer.close(index);
			},
			btn2: function(index) {
				layer.close(index);
			}
		});
	} else {
		layer.msg(res.error, {
			icon: 2,
			time: 800
		}, function() {
			if(res.status == 401) window.location.href = '/member/login.html';
		});
	}
}

// 活動報名回調
function __complete_actup(res) {
	layer.closeAll('loading');
	if(res.status == 201) {
		layer.msg('报名成功', {
			icon: 1,
			time: 1200
		}, function() {
			window.location.reload();
		});
	} else {
		layer.msg(res.error, {
			icon: 2,
			time: 800
		});
	}
}

// 活動取消報名
function deSingOut() {
	layer.load(0, {
		shade: [0.08, '#000']
	});
	__getdata_actout['activity_id'] = actid;
	factory.request('_activity_out');
}

// 活動取消報名回調
function __complete_actout(res) {
	layer.closeAll('loading');
	if(res.status == 204) {
		layer.msg('报名已取消', {
			icon: 1,
			time: 1200
		}, function() {
			window.location.reload();
		});
	} else {
		layer.msg(res.error, {
			icon: 2,
			time: 800
		});
	}
}